/**   
 * @文件名称: BaseServiceImpl.java
 * @类路径: cn.common.base.impl
 * @描述: TODO
 * @公司：春城1网
 * @作者：李兴建
 * @时间：2013-9-29 下午02:01:00
 * @版本：V1.0   
 */

package cn.ccew.common.base.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.ccew.common.base.dao.IBaseDao;
import cn.ccew.common.base.model.BaseEntity;
import cn.ccew.common.utils.CopyUtils;
import cn.ccew.common.utils.Page;

/**
 * @类功能说明：
 * @类修改者：
 * @修改日期：
 * @修改说明：
 * @公司：春城1网
 * @作者：李兴建
 * @创建时间：2013-9-29 下午02:01:00
 * @版本：V1.0
 */
@Service("baseService")
public class BaseServiceImpl implements IBaseService {
	@Autowired
	@Qualifier("baseDao")
	private IBaseDao baseDao;

	public Criteria createCriteria(Class<? extends BaseEntity> clazz) throws Exception {
		return baseDao.createCriteria(clazz);
	}

	public void delete(String id, Class<? extends BaseEntity> clazz) throws Exception {
		baseDao.delete(id, clazz);
	}

	public <T extends BaseEntity> List<T> findAll(Class<T> clazz) throws Exception {
		return baseDao.findAll(clazz);
	}

	public Page findBy(Query query, int pageNumber, int pageSize) throws Exception {
		return this.baseDao.findBy(query, pageNumber, pageSize);
	}

	public Page findBy(Criteria criteria, int pageNumber, int pageSize) throws Exception {
		return this.baseDao.findBy(criteria, pageNumber, pageSize);
	}

	public <T extends BaseEntity> T findById(String id, Class<T> clazz) throws Exception {
		return baseDao.findById(id, clazz);
	}

	public <T extends BaseEntity> List<T> findListByProperty(String propertyName, Object value, Class<T> clazz) throws Exception {
		return baseDao.findByProperty(propertyName, value, clazz);
	}

	public <T extends BaseEntity> T findObjByProperty(String propertyName, Object value, Class<T> clazz) throws Exception {
		List<T> list = baseDao.findByProperty(propertyName, value, clazz);
		if (null != list && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	public List<Object> findBySql(String sql) throws Exception {
		return baseDao.findBySql(sql);
	}

	public Page findPageBy(Criteria criteria, int pageNumber, int pageSize) throws Exception {
		return this.baseDao.findBy(criteria, pageNumber, pageSize);
	}

	public boolean isExist(String id, Class<? extends BaseEntity> clazz) throws Exception {
		return baseDao.isExist(id, clazz);
	}

	public void saveOrUpdate(BaseEntity bean) throws Exception {
		/*
		 * if (dbType.length > 0) { if (StringUtils.isNotEmpty(dbType[0])) {
		 * CustomerContextHolder.setCustomerType(dbType[0]); } }
		 */
		if (StringUtils.isNotEmpty(bean.getId())) {
			BaseEntity old = this.baseDao.findById(bean.getId(), bean.getClass());
			CopyUtils.copyNotNullProperties(old, bean);
			bean = old;
			this.baseDao.merge(bean);
		} else {
			this.baseDao.save(bean);
		}

	}

	@Override
	public int deleteAll(Class<? extends BaseEntity> clazz) {
		// TODO Auto-generated method stub
		return this.baseDao.deleteAll(clazz);
	}

}
